<?php
namespace App\Services;


use App\Models\Account;
use App\Models\AccountLog;
use Illuminate\Support\Facades\DB;

/**
 *
 * 用户账户服务层
 *
 * @author shengl@maqu.im
 *
 */
class UserAccountService extends BaseService {


    /**
     *  账户收入
     *
     * @param $account_id 账户ID
     * @param $account_type 账户类型
     * @param $amount 金额
     * @param $desc 描述
     * @param $order_id 订单id
     * @param $order_type 订单类型
     * @return array
     * @throws \Exception
     */
    public function incomeMoney($account_id,$amount,$desc,$order_id=0,$order_type='default',$income_type='&'){

        //获取用户账户信息
        $account = Account::where('account_id',$account_id)->first();

        if(!$account){
            return $this->failure("该账户不存在.");
        }

        //金额增加
        $account->money+=$amount;
        $account->update_at=gmtime();

        //数据入库
        DB::table('account')
            ->where('account_id',$account_id)
            ->update($account->toArray());

        //记录账户操作日志account_log
        $log = new AccountLog();
        $log->account_id = $account_id;
        $log->money = $account->money;
        $log->frozen_money = $account->frozen_money?$account->frozen_money:0;
        $log->change_money = $amount;
        $log->change_time = gmtime();
        $log->change_desc = $desc;
        $log->income_type = $income_type;
        $log->order_id = $order_id;
        $log->order_type = $order_type;
        $log->save();

        return $this->success();

    }

    /**
     * 发起申请时冻结申请提现的金额
     * @param $account_id
     * @param $account_type
     * @param $amount
     * @param $desc
     * @param bool|true $withTrans
     * @return array
     * @throws \Exception
     */
    public function frozenMoney($account_id,$amount,$desc,$withTrans =true){

        if($withTrans){
            DB::beginTransaction();
        }

        try{

            //获取账户信息
            $account =Account::where('account_id',$account_id)
                ->first();

            if(!$account){
                return $this->failure("账户不存在");
            }
            $account->frozen_money+=$amount;
            $account->money-=$amount;
            $account->update_at=gmtime();

            //更新用户的提现金额到冻结金额中
            DB::table('account')
                ->where('account_id',$account_id)
                ->update($account->toArray());

            //操作记录入库
            $log = new AccountLog();
            $log->account_id = $account_id;
            $log->money = $account->money;
            $log->frozen_money = $account->frozen_money?$account->frozen_money:0;
            $log->change_money = $amount;
            $log->change_time = gmtime();
            $log->change_desc = $desc;
            $log->income_type = '*';

            $log->save();

            if($withTrans){
                DB::commit();
            }

        } catch(\Exception $e){
            if($withTrans){
                DB::rollback();
            }
            throw $e;
        }

    }

    /**
     * 同意提现申请
     * @param $account_id
     * @param $amount
     * @param $desc
     * @param bool|true $withTrans
     * @return array
     * @throws \Exception
     */
    public function AgreeTakeApply($account_id,$amount,$desc,$withTrans =true){

        if($withTrans){
            DB::beginTransaction();
        }

        try{

            //获取用户账户
            $account =Account::where('account_id',$account_id)
                ->first();

            if(!$account){
                return $this->failure("账户不存在");
            }
            $account->frozen_money-=$amount;
            $account->update_at=gmtime();

            //账户操作

            DB::table('account')
                ->where('account_id',$account_id)
                ->update($account->toArray());

            //账户操作记录
            $log = new AccountLog();
            $log->account_id = $account_id;
            $log->money = $account->money;
            $log->frozen_money = $account->frozen_money?$account->frozen_money:0;
            $log->change_money = $amount;
            $log->change_time = gmtime();
            $log->change_desc = $desc;
            $log->income_type = '-';

            $log->save();

            if($withTrans){
                DB::commit();
            }

        } catch(\Exception $e){
            if($withTrans){
                DB::rollback();
            }
            throw $e;
        }

    }
    /**
     * 拒绝提现申请 解冻金额退回账户
     * @param $account_id
     * @param $amount
     * @param $desc
     * @param bool|true $withTrans
     * @return array
     * @throws \Exception
     */
    public function unfrozen($account_id,$amount,$desc,$withTrans =true){

        if($withTrans){
            DB::beginTransaction();
        }

        try{

            //用户账户
            $account = Account::where('account_id',$account_id)->first();

            if(!$account){
                return $this->failure("账户不存在.");
            }
            $account->frozen_money-=$amount;
            $account->money+=$amount;
            $account->update_at=time();

            //更新用户账户余额
            DB::table('account')
                ->where('account_id',$account_id)
                ->update($account->toArray());

            //账户操作记录
            $log = new AccountLog();
            $log->account_id = $account_id;
            $log->money = $account->money;
            $log->frozen_money = $account->frozen_money?$account->frozen_money:0;
            $log->change_money = $amount;
            $log->change_time = gmtime();
            $log->change_desc = $desc;
            $log->income_type = '*';

            $log->save();

            if($withTrans){
                DB::commit();
            }

        } catch(\Exception $e){
            if($withTrans){
                DB::rollback();
            }
            throw $e;
        }

    }


}
